package com.netease.yunxin.kit.chatkit.ui.dialog

import android.content.Context
import com.lxj.xpopup.impl.FullScreenPopupView
import com.netease.yunxin.kit.chatkit.ui.R
import com.netease.yunxin.kit.chatkit.ui.databinding.DialogGiftGifBinding
import com.opensource.svgaplayer.SVGACallback
import com.opensource.svgaplayer.SVGADrawable
import com.opensource.svgaplayer.SVGAParser
import com.opensource.svgaplayer.SVGAVideoEntity
import java.net.URL


class GiftGifDialog(context: Context,val gifUrl: String) : FullScreenPopupView(context) {
    var mViewBinding :DialogGiftGifBinding? = null

    override fun getImplLayoutId(): Int = R.layout.dialog_gift_gif

    override fun onCreate() {
        super.onCreate()
        mViewBinding = DialogGiftGifBinding.bind(popupImplView)
        SVGAParser(context).decodeFromURL(URL(gifUrl),object :
            SVGAParser.ParseCompletion {
            override fun onComplete(videoItem: SVGAVideoEntity) {
                val drawable = SVGADrawable(videoItem)
                mViewBinding?.svgImageView?.setImageDrawable(drawable)
                mViewBinding?.svgImageView?.startAnimation()
            }

            override fun onError() {
                dismiss()
            }
        })
        mViewBinding?.svgImageView?.callback = object : SVGACallback{
            override fun onFinished() {
                dismiss()
            }

            override fun onPause() {}

            override fun onRepeat() {}

            override fun onStep(frame: Int, percentage: Double) {}
        }
    }
}